package com.sls.web.manager.game.service.impl;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.OrderLogInfo;
import com.sls.web.manager.game.querybean.OrderLogInfoQuery;
import com.sls.web.manager.game.service.OrderLogInfoService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 订单日志Impl
 * 
 * @author gesx
 * 
 */
@Repository("orderLogInfoService")
public class OrderLogInfoServiceImpl extends SuperService implements
		OrderLogInfoService {
	private static Logger log = Logger.getLogger(OrderLogInfoServiceImpl.class);
	private Gson gson;

	public OrderLogInfoServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public Pagination<OrderLogInfo> queryList(
			OrderLogInfoQuery orderLogInfoQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ORDER_LOG_INFO_LIST);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, orderLogInfoQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<OrderLogInfo> order = gson.fromJson(body,
					new TypeToken<Pagination<OrderLogInfo>>() {
					}.getType());
			return order;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public OrderLogInfo queryById(String Id, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ORDER_LOG_INFO_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OrderLogInfo order = new OrderLogInfo();
		order.setId(Id);
		SocketDTO dto = super.send(head, order);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			OrderLogInfo order2 = gson.fromJson(body, OrderLogInfo.class);
			return order2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	public static Logger getLog() {
		return log;
	}

	public static void setLog(Logger log) {
		OrderLogInfoServiceImpl.log = log;
	}

}
